package com.ruoyi.develop.lvDaohuo.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuy.service.IGoodsSalebuyService;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSalebuyall.mapper.GoodsSalebuyallMapper;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledo.mapper.GoodsSaledoMapper;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.mapper.LvBuyMapper;
import com.ruoyi.develop.lvDaohuoDetail.domain.LvDaohuoDetail;
import com.ruoyi.develop.lvDaohuoDetail.mapper.LvDaohuoDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvDaohuo.mapper.LvDaohuoMapper;
import com.ruoyi.develop.lvDaohuo.domain.LvDaohuo;
import com.ruoyi.develop.lvDaohuo.service.ILvDaohuoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 到货单Service业务层处理
 * @author qh
 * @date 2024-09-05
 */
@Service
public class LvDaohuoServiceImpl implements ILvDaohuoService {
    @Resource
    private LvDaohuoMapper lvDaohuoMapper;
    @Resource
    private LvDaohuoDetailMapper lvDaohuoDetailMapper;
    @Resource
    private GoodsSalebuyallMapper goodsSalebuyallMapper;

    @Resource
    private LvBuyMapper lvBuyMapper;
    @Autowired
    private IGoodsSalebuyService goodsSalebuyService;

    @Autowired
    private IGoodsSaleService goodsSaleService;

    @Autowired
    private GoodsSaledoMapper goodsSaledoMapper;
    /**
     * 查询到货单
     * @param id 到货单主键
     * @return 到货单
     */
    @Override
    public LvDaohuo selectById(String id) {
        return lvDaohuoMapper.selectById(id);
    }

    /**
     * 查询到货单列表
     * @param entity 到货单
     * @return 到货单
     */
    @Override
    public List<LvDaohuo> selectList(LvDaohuo entity) {
        return lvDaohuoMapper.selectList(entity);
    }

    @Override
    public List<LvDaohuo> listYunfei(LvDaohuo entity) {
        return lvDaohuoMapper.listYunfei(entity);
    }
    /**
     * 新增到货
     * @param entity 到货单
     * @return 结果
     */
    @Override
    @Transactional
    public int insert(LvDaohuo entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.isBlank(entity.getNos())){
            Date arrivalDate = entity.getArrivalDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(arrivalDate==null?new Date():arrivalDate);
            String codeMaxString = lvDaohuoMapper.selectMaxCode(today);
            if(StringUtils.isNotBlank(codeMaxString)){
                // 最大编号+1
                codeMaxString = Long.parseLong(codeMaxString)+1+"";
            }else{
                codeMaxString = today+"0001";
            }
            entity.setNos(codeMaxString);
        }
        return lvDaohuoMapper.insert(entity);
    }

    @Override
    @Transactional
    public int insertZhifahuo(LvDaohuo entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        String codeMaxString = lvBuyMapper.selectMaxCode(today);
        if(StringUtils.isNotBlank(codeMaxString)){
            // 最大编号+1
            codeMaxString = Long.parseLong(codeMaxString)+1+"";
        }else{
            codeMaxString = today+"0001";
        }
        entity.setNos(codeMaxString);

        entity.setFahuoType(BusinessConstants.FAHUO_TYPE_1);//直发货
        LvBuy lvBuy = lvBuyMapper.selectById(entity.getBuyId());
        if(lvBuy!=null){
            entity.setSupplierId(lvBuy.getSupplierId());
            entity.setSupplierName(lvBuy.getSupplierName());
            entity.setAccountNo(lvBuy.getAccountNo());
            entity.setBankName(lvBuy.getBankName());
            entity.setBankNo(lvBuy.getBankNo());
        }

        int i= lvDaohuoMapper.insert(entity);
        try {
            List<GoodsSalebuyall> goodsSalebuyallList = entity.getGoodsSalebuyallList();

            BigDecimal totalPrice = BigDecimal.ZERO;
            for (GoodsSalebuyall goodsSalebuyall : goodsSalebuyallList) {
                // 等于0的是没有到货 不需要处理
                if(goodsSalebuyall.getDaohuoCountTemp().compareTo(BigDecimal.ZERO) != 0){
                    LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
                    lvDaohuoDetail.setSaleId(goodsSalebuyall.getId());
                    lvDaohuoDetail.setId(IdUtils.fastSimpleUUID());
                    lvDaohuoDetail.setDaohuoId(entity.getId());
                    lvDaohuoDetail.setGoodId(goodsSalebuyall.getGoodsId());
                    lvDaohuoDetail.setCreateBy(SecurityUtils.getUsername());
                    lvDaohuoDetail.setCreateTime(DateUtils.getNowDate());
                    lvDaohuoDetail.setDaohuoCount(goodsSalebuyall.getDaohuoCountTemp());
                    lvDaohuoDetail.setUnitPrice(goodsSalebuyall.getGoodsBuyPrice());
                    lvDaohuoDetail.setTotalPrice(goodsSalebuyall.getDaohuoCountTemp().multiply(goodsSalebuyall.getGoodsBuyPrice()));
                    lvDaohuoDetail.setFahuoType(BusinessConstants.FAHUO_TYPE_1);//直发货
                    lvDaohuoDetail.setInspectState(BusinessConstants.INSPECT_STATE_YES);//已质检
                    lvDaohuoDetailMapper.insert(lvDaohuoDetail);

                    totalPrice = totalPrice.add(lvDaohuoDetail.getTotalPrice());

                    // 更新商品的 到货情况
                    BigDecimal shengyuDaohuoCount = goodsSalebuyall.getShengyuDaohuoCount().subtract(goodsSalebuyall.getDaohuoCountTemp());

                    // 如果剩余到货数量为小于等于0，则更新采购表的状态为全部到货
                    if (shengyuDaohuoCount.compareTo(BigDecimal.ZERO) <= 0) {
                        goodsSalebuyall.setShengyuDaohuoCount(BigDecimal.ZERO);
//                        goodsSalebuyall.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_ALL);
                    }
                    goodsSalebuyall.setDaohuoId(entity.getId());
                    goodsSalebuyall.setDaohuono(entity.getNos());
                    goodsSalebuyall.setShengyuDaohuoCount(shengyuDaohuoCount);
                    //goodsSalebuyall.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_PART);//部分到货
                    goodsSalebuyall.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_ALL);//全部到货
                    BigDecimal daohuoCount = goodsSalebuyall.getDaohuoCount().add(goodsSalebuyall.getDaohuoCountTemp());
                    goodsSalebuyall.setDaohuoCount(daohuoCount);
                    // 实际到货数量 大于 实际采购数量
                    goodsSalebuyallMapper.update(goodsSalebuyall);
                }
            }

            lvBuy.setMoney(totalPrice);
            lvBuy.setShengyu(totalPrice);
            lvBuy.setArrears(lvBuy.getMoney().subtract(lvBuy.getDingjin()));
            lvBuyMapper.update(lvBuy);

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return i;
    }
    /**
     * 修改到货单
     * @param entity 到货单
     * @return 结果
     */
    @Override
    public int update(LvDaohuo entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return lvDaohuoMapper.update(entity);
    }

    @Override
    public int updateYf(LvDaohuo entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        entity.setDdSongpeople(SecurityUtils.getUsername());
        entity.setDdDatepeople(DateUtils.getNowDate());
        entity.setIsSong("已打款");
        return lvDaohuoMapper.update(entity);
    }
    /**
     * 批量删除到货单
     * @param ids 需要删除的到货单主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return lvDaohuoMapper.deleteByIds(ids);
    }

    /**
     * 删除到货单信息
     * @param id 到货单主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvDaohuoMapper.deleteById(id);
    }

    @Override
    public String selectMaxCode(String today) {
        return lvDaohuoMapper.selectMaxCode(today);
    }
}
